package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

const (
	USERNAME = "root"
	PASSWORD = "root"
	HOST     = "127.0.0.1"
	PORT     = 3306
	DATABASE = "blog"
)

var DB *gorm.DB

func init() {

}
func InitMysql() {
	var (
		err error
		dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
			USERNAME, PASSWORD, HOST, PORT, DATABASE,
		)
	)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
			TablePrefix:   "week1_",
		},
	})
	if err != nil {
		panic(err)
	}
	err = DB.AutoMigrate(&Goods{}, &Order{}, &OrderItems{}, &OrderAddress{})
	if err != nil {
		panic(err)
	}
}

// 商品表
type Goods struct {
	gorm.Model
	GoodsName   string
	GoodsPrice  float64
	GoodsStock  int
	GoodsDesc   string
	GoodsStatus int
}

// 订单表
type Order struct {
	gorm.Model
	OrderSn        string
	UserId         int
	OrderTotal     float64 `gorm:"type:decimal(7,0);not null;comment:订单总价"`
	OrderAddressId uint
	OrderStatus    int `gorm:"type:tinyint(1);not null;default:0;check:order_status IN (0,1);comment:订单支付状态"`
}

// 订单详情表
type OrderItems struct {
	gorm.Model
	OrderSn    string
	GoodId     int
	GoodsStock int
	GoodsPrice float64 `gorm:"type:decimal(7,0);not null;comment:订单单价"`
}

// 订单地址表
type OrderAddress struct {
	gorm.Model
	UserId      int
	AddressName string
}
